<template>
  <div class="container">
    <div class="app-container">
      <!-- 标题区 -->
      <div class="title">
        <el-button size="mini	" type="primary" @click="isShow = true">添加角色</el-button>
      </div>
      <!-- 分页区 -->
      <el-table
        :data="list"
        border
        style="width: 100%"
      >
        <el-table-column
          fixed
          prop="id"
          label="序号"
          width="50"
          align="center"
        />
        <el-table-column
          prop="name"
          label="角色"
          width="200"
          align="center"
        >
          <template v-slot="{row}">
            <el-input v-if="row.isEdit" v-model="row.editROw.name" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="state"
          label="启用"
          width="200"
          align="center"
        >
          <template v-slot="{row}">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editROw.state"
              :active-value="1"
              :inactive-value="0"
            />
            <template v-else>
              <span v-if="row.state===1">启用</span>
              <span v-else-if="row.state===0">未启用</span>
              <span v-else>无</span>
            </template>
          </template>
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述"
          align="center"
        >
          <template v-slot="{row}">
            <el-input v-if="row.isEdit" v-model="row.editROw.description" type="textarea" style="width: 300px;" size="mini" :rows="2" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
        >
          <template v-slot="{row}">
            <template v-if="row.isEdit">
              <el-button size="mini" type="primary" @click="editFn(row.id)">确认</el-button>
              <el-button size="mini" @click="huifu(row)">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text">分配权限</el-button>
              <el-button type="text" @click="row.isEdit=true">编辑</el-button>

              <el-popconfirm
                title="确定要删除吗？"
                @onConfirm="delRoleFn(row.id)"
              ><el-button slot="reference" style="margin-left:10px;" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-row type="flex" justify="center" class="footer">
        <el-pagination
          :current-page.sync="pageData.page"
          :page-size="pageData.pagesize"
          layout=" prev, pager, next"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <!-- 添加弹窗 -->
    <el-dialog width="500px" title="新增角色" :visible.sync="isShow" @close="closeFn">
      <el-form ref="form" label-width="120px" :rules="rules" :model="form">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name" autocomplete="off" size="mini" style="width: 300px;" />
        </el-form-item>
        <el-form-item label="启用" prop="state">
          <el-switch
            v-model="form.state"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="form.description" style="width: 300px;" size="mini" rows="3" autocomplete="off" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="closeFn">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleApi, addRoleApi, delRoleApi, editRoleApi } from '@/api/role'
export default {
  name: 'Role',

  data() {
    return {
      isShow: false,
      total: 1,
      list: [],
      pageData: {
        page: 1,
        pagesize: 5
      },
      form: {
        name: '',
        description: '',
        state: true
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名字', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入描述内容', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.initRole()
  },
  methods: {
    async submit() {
      try {
        await this.$refs.form.validate()
        await addRoleApi(this.form)
        this.$message.success('新增角色成功')
        this.pageData.page = 1
        this.initRole()
        this.closeFn()
        // console.log(this.form)
      } catch (error) {
        // console.log(error)
      }
    },
    closeFn() {
      this.$refs.form.resetFields()
      this.isShow = false
    },
    async  initRole() {
      const res = await getRoleApi(this.pageData)
      // console.log(res)
      res.rows.forEach(item => {
        item.isEdit = false
        item.editROw = {
          description: item.description,
          name: item.name,
          state: item.state
        }
      })
      this.list = res.rows

      this.total = res.total
    },

    handleCurrentChange(page) {
      this.pageData.page = page
      this.initRole()
    },
    async delRoleFn(id) {
      await delRoleApi(id)
      this.$message.success('删除成功')
      if (this.list.length === 1 && this.pageData.page > 1) {
        this.pageData.page--
      }
      this.initRole()
    },
    huifu(row) {
      row.isEdit = false
      row.editROw = {
        name: row.name,
        description: row.description,
        state: row.state
      }
    },
    async editFn(id) {
      const index = this.list.findIndex(item => item.id === id)
      if (this.list[index].editROw.name && this.list[index].editROw.description) {
        await editRoleApi({ ...this.list[index].editROw, id })
        this.initRole()
        this.$message.success('更新角色成功')
      } else {
        this.$message.warning('角色名称和描述不能为空')
      }
    }
  }
}

</script>
<style lang="scss" scoped>
.container{
   padding: 20px;
   .app-container{
      padding:20px;
      .title{
          margin-bottom: 10px;
}
}
}

</style>
